#!/bin/bash
functions_svn_included=true
if [ "$functions_base_included" = "" ]; then
file_name=functions-base.sh
funcs=`which $file_name`
if [ "$funcs" != "" ] ;then
  #echo "funcs=$funcs"
  . $funcs
else
  funcs=`dirname $0`/$file_name
  #echo "$PWD"
  #echo "funcs=$funcs"
  if [ "$funcs" != "" ] ;then
    . $funcs
  else
    exit
  fi
fi
fi
#一些常用参数的设置
###################################
#一些常用参数的设置
###################################
#default_repos_root=$HOME/repos
default_repos_root=/mnt/15/repos
default_repos_name=smartphone
default_backup_root=$HOME/repos/backup
default_mode="auto"
default_frequency=14

repoes_list="
$default_repos_root/DocsAndTools 
$default_repos_root/settings
"
######################################
SVNADMIN="$(which svnadmin)"
SVNLOOK="$(which svnlook)"
MYDATE="$(date +%Y%m%d)"
TAR="$(which tar)"
MV="$(which mv)"
RM="$(which rm)"
SCP="$(which scp)"
SVN_BACPUP_SH=/usr/local/bin/svn-backup-select.sh
#>>>>>>>>>>scm配置管理相关
function sh_get_svn_host()
{
  local ac_172_20=`sh_get_host_ip |grep 172.20`
  local ac_192_168_0=`sh_get_host_ip  |grep 192.168.0`
  local ac_192_168_1=`sh_get_host_ip  |grep 192.168.1`
  local svn_host=
  #echo "aa=$ac_172_20 bb=$ac_192_168_0 cc=$ac_192_168_1" >&2
  if [ -n "$ac_172_20" ]; then
    svn_host=http://smartphone
  elif [ -n "$ac_192_168_0" ]; then
    svn_host=http://svn
  elif [ -n "$ac_192_168_1" ]; then
    svn_host=http://svn
  else
    svn_host=http://smartphone
  fi
  echo "$svn_host"
}

function sh_get_svn_hostip()
{
  local ac_172_20=`sh_get_host_ip |grep 172.20`
  local ac_192_168_0=`sh_get_host_ip  |grep 192.168.0`
  local ac_192_168_1=`sh_get_host_ip  |grep 192.168.1`	
  local svn_host=
 # echo "aa=$ac_172_20 bb=$ac_192_168_0 cc=$ac_192_168_1" >&2
  if [ -n "$ac_172_20" ]; then
    svn_host=smartphone
  elif [ -n "$ac_192_168_0" ]; then
    svn_host=svn
  elif [ -n "$ac_192_168_1" ]; then
    svn_host=192.168.1.123
  else
    svn_host=smartphone
  fi
  echo "$svn_host"
}


function sh_mount_storage_cabinet()
{
    local return_value=
    smbfs_info=`dpkg -s smbfs|grep "install ok installed"`
    if [ "$smbfs_info" = "" ]; then
        sudo apt-get install smbfs
    fi
    remote_dir=//172.20.126.223/smartphone/
    mount_point=$HOME/repos/storage
    if test ! -d $mount_point; then
        mkdir -pv $mount_point
    fi
    args="-o iocharset=utf8,username=mhf,password=dg123456,uid=$USER,gid=$USER"
    mount_info=(`mount |grep storage`)
    if [ "$mount_info" = "" ]; then
        if sh_at_comp; then
            sudo mount -t smbfs $args $remote_dir $mount_point
        fi
        return_value=$mount_point
    else
        return_value=${mount_info[2]}
    fi
    echo ${return_value}
}


function is_svn_working_copy()
{  
    if test -d $1/.svn ;then
        return 0
    else
        return 1
    fi
}

function sh_get_svn_url()
{
    local ac_url=
    echo "is_svn_working_copy $1" >&2
    if is_svn_working_copy $1; then
        ac_url=`svn.shc info  $1 |grep "URL:" |awk '{print $2}'`
    else
	ac_url=`svn.shc info  $1 |grep "URL:" |awk '{print $2}'`
    fi
    echo $ac_url
}

function sh_url_is_valid()
{
  echo "dd"
}


function sh_get_svn_url_root()
{
    local ac_url_root=
    if is_svn_working_copy $1; then
        ac_url_root=`svn.shc info  $1 | sed -n '3p' |awk '{print $2}'`
    fi
    echo $ac_url_root
}

function sh_get_svn_committed_no_from_url()
{
    local ac_return=
    unset_color
    if [ -n "$1" ]; then
        ac_return=`svn.shc info  $1   | sed -n '8p' |awk '{print $2}'`
    fi
    echo $ac_return
}


function sh_get_svn_head_no_from_url()
{
    local ac_return=
    unset_color
    if [ -n "$1" ]; then
        ac_return=`svn.shc info  $1 | sed -n '5p' |awk '{print $2}'`
    fi
    echo $ac_return
}

function sh_get_svn_committed_no_from_copy()
{
    local ac_return=
    unset_color
    ac_url=`sh_get_svn_url $1`
    if [ -n "$ac_url" ]; then
        ac_return=`svn.shc info  $ac_url | sed -n '8p' |awk '{print $2}'`
    fi
    echo $ac_return
}


function sh_is_url()
{
  local result=
  local svn_url=$1
  result=`echo $svn_url |grep http  2>/tmp/error.txt`
  if [ -n "$result" ]; then
    return 0
  else
    return 1
  fi
}



function sh_get_svn_committed_no_of_copy()
{
    local ac_return=
    unset_color
    local work_copy=$1
    if [ -n "$work_copy" ]; then
        if `sh_is_url $work_copy`; then
          ac_return=`svn.shc info  $work_copy |sed -n '8p' |awk '{print $2}'`
        else
	  ac_return=`svn.shc info  $work_copy |sed -n '9p' |awk '{print $2}'`
        fi
    fi
    echo $ac_return
}


function sh_get_svn_head_no_from_copy()
{
    local ac_return=
    unset_color
    ac_url=`sh_get_svn_url $1`
    if [ -n "$ac_url" ]; then
        ac_return=`svn.shc info  $ac_url |sed -n '8p' |awk '{print $2}'`
    fi
    echo $ac_return
}

function sh_get_svn_head_no_of_copy()
{
    local ac_return=
    unset_color
    local work_copy=$1
    if [ -n "$work_copy" ]; then
        if `sh_is_url $work_copy`; then
          ac_return=`svn.shc info  $work_copy |sed -n '8p' |awk '{print $2}'`
        else
	  ac_return=`svn.shc info  $work_copy |sed -n '9p' |awk '{print $2}'`
        fi
    fi
    echo $ac_return
}


#*********************************************************************************
# 从一段log信息中提出svn版本号
#*********************************************************************************
function sh_get_svn_no_from_log()
{
  unset_color
  local result=
  if [ -z $1 ] || [ -z $2 ]; then
    echo $result
    return
  fi
  result=`svn.shc log -r $2 $1 --xml||grep ^r |awk '{print $1}'`
  result=`echo ${var#*r}`
  echo "$result"
}

#*********************************************************************************
# 从一段log信息中提出svn版本号
#*********************************************************************************
function sh_get_svn_log_by_no()
{
  local work_copy=$1
  local version=$2
  svn.shc log $work_copy -r $2 -v
}




#指明： 路径  ［commited|head|prev] 等获取版本号
function sh_get_svn_version_no_from_log()
{
  sh_get_svn_no_from_log $1 $2 "$3"
}



#*********************************************************************************
##从log信息中获取提交者信息
#eg:
#sh_get_author_from_svn_log  /opt/workspace/gphone/mtk6573/alps-w1140-20111007 1279
#*********************************************************************************
function sh_get_author_from_svn_log()
{
  unset_color
  local result=
  if [ -z $1 ] || [ -z $2 ]; then
    echo $result
    return
  fi
  result=`svn.shc log -r $2 $1 --xml|grep author -w`
  result=`echo $result |sed "s#author##g" |tr -d '\<\>\/'`
  echo "$result"
}

#*********************************************************************************
#从log信息中获取版本号码
#eg:
#sh_get_date_from_svn_log /opt/workspace/gphone/mtk6573/alps-w1140-20111007 1279
#*********************************************************************************
function sh_get_date_from_svn_log()
{
  unset_color
  local result=
  if [ -z $1 ] || [ -z $2 ]; then
    echo $result
    return
  fi
  result=`svn.shc log -r $2 $1 --xml|grep date -w`
  result=`echo $ac_return |sed "s#date##g" |tr -d '\<\>\/'`
  echo "$ac_return"
}

function sh_get_svn_version_min_from_string() 
{
local _tmp_version=$1
_min_version=`echo $_tmp_version |awk -F ':' '{print $1}'`
echo $_min_version
}

function sh_get_svn_version_max_from_string() 
{
local _tmp_version=$1
_max_version=`echo $_tmp_version |awk -F ':' '{if($2==""){print $1} else {print $2}}'`
echo $_max_version
}


#*********************************************************************************
#判断url是否有效
#eg:
#sh_get_date_from_svn_log /opt/workspace/gphone/mtk6573/alps-w1140-20111007 1279
#*********************************************************************************
function sh_url_is_exist()
{
  local ac_exist=
  local svn_url=$1
  ac_exist=`svn.shc list  $svn_url/ 2>/tmp/error.txt`
  if [ -n "$ac_exist" ]; then
    return 0
  else
    return 1
  fi
}




#*********************************************************************************
#sh_svn_up $path/to/work_copy --url $url --username $user --password $pwd -r 51817
#sh_svn_up $path/to/work_copy --username $user --password $pwd -r 51817
#*********************************************************************************
function sh_svn_up() 
{
  local svn_version=
  while test $# != 0
  do
    case $1 in
	--username)
	    shift
	    username=$1
	    ;; 
	--password)
	    shift
	    password=$1
	    ;; 
	-m)
	    shift
	    mode=$1
	    ;; 
        --url)
	    shift
	    url=$1
	    ;;
        --dir)
	    shift
	    work_dir=$1
	    ;;
        -r)
	    shift
	    svn_version="-r $1"
	    ;;

        *)
	    work_dir=$1
	   ;;
       esac
       shift
  done

  if [ -n "$url" ]; then
    if test -d $work_dir; then
      if test -d $work_dir/.svn; then
        if [ "$url" = "`sh_get_svn_url $work_dir`" ]; then
          echo "svn.shc up  $work_dir  $svn_version"
          svn.shc up  $work_dir  $svn_version
        else
	  echoTextRed "$work_dir 不是指定url: $url 的工作拷贝!!"
          echoTextGreen "$work_dir url: `sh_get_svn_url $work_dir`"
        fi
      else
        svn.shc co $url  $work_dir  $svn_version
      fi
    else
      echo "svn.shc co $url $work_dir  $svn_version"
      svn.shc co $url  $work_dir  $svn_version
    fi
  else
    if test -d $work_dir; then
      if test -d $work_dir/.svn; then
        echo "svn.shc up  $work_dir  $svn_version"
        svn.shc up  $work_dir  $svn_version
      fi
    else
      echo "svn.shc co $url $work_dir  $svn_version"
      svn.shc co $url  $work_dir  $svn_version
    fi
  fi
}

#*********************************************************************************
#更新android框架代码
#*********************************************************************************
function sh_svn_up_frameworks() 
{
  echoTextBlue "aaa开始更新框架代码......"
  local svn_version=
  while test $# != 0
  do
    case $1 in
	--username)
	    shift
	    username=$1
	    ;; 
	--password)
	    shift
	    password=$1
	    ;; 
	-m)
	    shift
	    mode=$1
	    ;; 
        --url)
	    shift
	    url=$1
	    ;;
        --dir)
	    shift
	    work_dir=$1
	    ;;
        -r)
	    shift
	    svn_version="-r $1"
	    ;;

        *)
	    work_dir=$1
	   ;;
       esac
       shift
  done

  url_of_work_dir=$work_dir
  if [ -n "$url" ]; then
    url_of_work_dir=`sh_get_svn_url  $work_dir ""`
  fi
  if [ -n "$svn_version" ]; then
    echo "$svn_version" |awk '{print $2}' >$work_dir/.version
    svn_version_cut_r=`echo $svn_version | awk '{print $2}'`
    sed -i "/current_framework_version/s/.*/current_framework_version:${svn_version_cut_r}/g" $work_dir/svn_version
  else
    svn_version=`sh_get_svn_committed_no_of_copy $url_of_work_dir`
    echo "$svn_version">$work_dir/.version
    sed -i "/current_framework_version/s/.*/current_framework_version:${svn_version}/g" $work_dir/svn_version
    svn_version="-r $svn_version"
  fi
 
  files_of_top_dir=`svn.shc list $url_of_work_dir   $svn_version |sed 's#out/##g' |sed 's#packages/##g'`

  #去掉路径信息最后的'/'
  url=`sh_delete_last_slash $url`
  work_dir=`sh_delete_last_slash $work_dir`
  if [ -n "$url" ]; then
    if test -d $work_dir; then
      if test -d $work_dir/.svn; then
        if [ "$url" = "$url_of_work_dir" ]; then
          for f in $files_of_top_dir
          do
            echo "svn.shc up $work_dir/$f  $svn_version"  
            svn.shc up  $work_dir/$f  $svn_version
          done
        else
	  echoTextRed "$work_dir 不是指定url: $url 的工作拷贝!!"
          echoTextGreen "工作拷贝 $work_dir url: $url_of_work_dir"
        fi
      else
        echo "svn.shc co $url $work_dir  $svn_version"
        svn.shc co $url  $work_dir  $svn_version
        if test -d  $work_dir/packages/; then
          rm -rf  $work_dir/packages/
        fi    
      fi
    else
      echo "svn.shc co $url $work_dir  $svn_version"
      svn.shc co $url  $work_dir  $svn_version
      if test -d  $work_dir/packages/; then
        rm -rf  $work_dir/packages/
      fi 
    fi
  else
    if test -d $work_dir; then
      if test -d $work_dir/.svn; then
        for f in $files_of_top_dir
        do
          echo "svn.shc up $work_dir/$f  $svn_version"
          svn.shc up $work_dir/$f  $svn_version
        done
      fi
    else
      echo "svn.shc co $url $work_dir $svn_version"
      svn.shc co $url  $work_dir  $svn_version
      if test -d  $work_dir/packages/; then
        rm -rf  $work_dir/packages/
      fi 
    fi
  fi
}


#更新第三方，用法:  sh_svn_up_thirdparty --work_copy  work_copy  --url  thirdparty_url  -p  project_name  -d  product_name
function sh_svn_up_thirdparty()
{
    echo "更新第三方.........."
    local work_copy=
    local thirdparty_url=
    local project_name=
    local thirdparty_ver=
    local product_name=
    
    while test $# != 0
    do
    case $1 in
	--work_copy |--work-copy)
	    shift
	    work_copy=$1
	    ;;
        --url)
	    shift
	    thirdparty_url=$1
	    ;;  
        -url)
	    shift
	    thirdparty_url=$1
	    ;; 
	-p)
	    shift
	    project_name=$1
	    ;;
	-r)
	    shift
	    thirdparty_ver=$1
	    ;;
	-d)
	    shift
	    product_name=$1
	    ;;
       esac
       shift
done
       # work_copy=$1
	isurl="`echo "$thirdparty_url" | grep http`"
	if [ "$isurl" != "" ];then
		SRC_SVN="$thirdparty_url"
	else
		SRC_SVN="http://smartphone/repositories/ThirdParty/Apks"
	fi
	if [ "$work_copy" != "" ];then
   		 CONFIG_PATH="${work_copy}/vendor/bbk/common/3Party"
	else
		CONFIG_PATH="vendor/bbk/common/3Party"
	fi
	echo "thirdparty_url=$thirdparty_url"
	rm -rf $CONFIG_PATH/
	BBK_PRODUCT_MODEL=$project_name
        echo $BBK_PRODUCT_MODEL
	CONFIG_FILE_NAME=${BBK_PRODUCT_MODEL}_Config.txt
	CONFIG_FILE=${CONFIG_PATH}/${CONFIG_FILE_NAME}

	#从第三方仓初始化到框架
	rm -rf $CONFIG_PATH
	if [ "$thirdparty_ver" = "" ];then
		echo "svn.shc co $SRC_SVN $CONFIG_PATH"
		svn.shc co $SRC_SVN $CONFIG_PATH
	else
		echo "svn.shc co $SRC_SVN $CONFIG_PATH -r $thirdparty_ver"
		svn.shc co $SRC_SVN $CONFIG_PATH -r $thirdparty_ver
	fi
	#删除在配置文件中不存在，但是在版本库中的存在的apk，做到不同项目中，有的apk并不使用
	apk_in_repo=`ls $CONFIG_PATH | grep "apk"`
	apk_in_config_file=`cat $CONFIG_FILE  | grep apk | awk -F '=' '{print $1}'`

	for i in $apk_in_repo ;do
	   result=`strIsSubstring "$i" "$apk_in_config_file"`
	   if [ "$result" -eq "0" ];then
		   echo "rm -f ${CONFIG_PATH}/${i}"
		   rm -f ${CONFIG_PATH}/${i}
	   else
		   echo "i=$i,result=1，不需要删除此apk"
	   fi
	done
	#按机型读取配置文件并更新到配置文件中apk的对应版本
	for var in `cat $CONFIG_FILE | grep -v "#"`;do
		  
		  apk_name=`echo $var | awk -F '=' '{print $1}'`
		  apk_svn_version=`echo $var | awk -F '=' '{print $2}'`

		  #echo "${CONFIG_PATH}/${apk_name}"
		  echo "svn.shc update ${SRC_SVN}/${apk_name} -r ${apk_svn_version}"
		  svn.shc up ${CONFIG_PATH}/${apk_name} -r ${apk_svn_version}
	done
	#更新android.mk文件
	echo "CONFIG_FILE_NAME=$CONFIG_FILE_NAME"
	echo "CONFIG_FILE=$CONFIG_FILE"
#	echo "mv ${CONFIG_PATH}/Android_${BBK_PRODUCT_MODEL}.mk ${CONFIG_PATH}/Android.mk"
#	mv ${CONFIG_PATH}/Android_${BBK_PRODUCT_MODEL}.mk ${CONFIG_PATH}/Android.mk
	bbk73=`echo $product_name |grep bbk73`
	bbk75=`echo $product_name |grep bbk75`
	bbk77=`echo $product_name |grep bbk77`
	if [ "$bbk73" != "" ];then
		echo "mv ${CONFIG_PATH}/Android_${BBK_PRODUCT_MODEL}.mk ${CONFIG_PATH}/Android.mk"
		mv -v ${CONFIG_PATH}/Android_${project_name}.mk ${CONFIG_PATH}/Android.mk
		rm -rvf ${CONFIG_PATH}/Android_*
	else
		rm -vf ${CONFIG_PATH}/*.mk
	fi
	rm -vf ${CONFIG_PATH}/*.txt
}

function sh_svn_up_modem() 
{
  echo ""
}


#编译测试
function sh_incremental_svn_up_all()
{
  echo "测试编译过程  ddddd"
  local work_copy=$1
  local product_name=$2
  local l_version_R=$3
  local l_version_r=$4
  local l_framework_head_v=
  local l_app_head_v=
  local FINDLEAVES=`which findleaves.py`
  local cmds=
  local files_list=

  if [ -z "$work_copy" ]; then
    work_copy=`pwd`
  fi  
  cd $work_copy
  local framework_url=`sh_get_svn_url $work_copy` 
  if [ -n "$l_version_R" ]; then
    l_version_R="-r $l_version_R"
  fi
  if [ -n "$l_version_r" ]; then
    l_version_r="-r $l_version_r"
  fi

  l_framework_head_v=`sh_get_svn_committed_no_from_url $framework_url`
  app_url=`sh_get_svn_url $work_copy/packages/apps/Phone |grep apps/Phone/Phone`
  if [ -n "$app_url" ]; then
    ac_replace=`basename $app_url`
    app_url=`sh_get_svn_url $work_copy/packages/apps/Phone |sed "s#/apps/Phone/$ac_replace##g"` 
  else
    app_url=`sh_get_svn_url $work_copy/packages`
  fi
  l_app_head_v=`sh_get_svn_committed_no_from_url $app_url`
  if test -f $work_copy/.version; then
    echo "$l_framework_head_v" > $work_copy/.version
  else
    touch $work_copy/.version
    echo "$l_framework_head_v" > $work_copy/.version
  fi
  if test -f $work_copy/packages/.version; then
    echo "$l_app_head_v" > $work_copy/packages/.version
  else
    touch $work_copy/packages/.version
    echo "$l_app_head_v" > $work_copy/packages/.version
  fi
  svn-log.sh  $framework_url $l_version_R:head  
  if test -f  ${HOME}/svnlog/${USER}-svn-log-files.txt; then
    files_list=`cat ${HOME}/svnlog/${USER}-svn-log-files.txt`
    for f in $files_list
    do
      svn.shc up $f -r $l_framework_head_v
    done      
  fi
  #更新应用
  if [ -n "$FINDLEAVES" ]; then
    cmds=`$FINDLEAVES packages/ Android.mk |sort -r |sed "s/Android.mk//g"`
  fi
  if [ -n "$cmds" ]; then
     echo "svn.shc up $cmds $l_app_head_v"
     svn.shc up $cmds $l_app_head_v
  fi
  cd -
}

function check_reposes_root()
{
if [ "$REPOS_ROOT" != "" ]; then
    export REPOS_ROOT
    echo $REPOS_ROOT
    return
fi
REPOS_ROOT=`question  "Please select the root directory of your svn repository ..."  2  ${default_repos_root}`
if test ! -d $REPOS_ROOT ; then
result=`question  "directory ${REPOS_ROOT} not exist， are you create it？"  1  yes`
if [ "$result" = "yes" ] ;then
    if test -w $REPOS_ROOT; then
        mkdir -pv $REPOS_ROOT
    else
        echoTextRed "no permission creat directory $REPOS_ROOT" >&2
    fi
fi
fi
export REPOS_ROOT
echo $REPOS_ROOT
}


function check_repos_name()
{
if [ "$REPOS_NAME" = "" ];then
REPOS_NAME=`question  "Please enter svn repository name ..."  2  xxx`
fi
export REPOS_NAME
echo $REPOS_NAME
}

function svn_create_repos()
{
echoText  "Begain to create svn repository ..." GREEN
check_reposes_root
check_repos_name
if test ! -d $REPOS_ROOT/${REPOS_NAME} ; then
svnadmin create --fs-type=fsfs  $REPOS_ROOT/${REPOS_NAME}
fi
}

function svn_config_repos()
{
echoText  "Begain to config svn repository  ..."  GREEN

#修改权限
#否则会出现：Can't open file "/opt/subversion/AT2/db/txn-current-lock" Permission denied
sudo chmod -R o+rw $REPOS_ROOT/${REPOS_NAME}

#修改 path_to_svn_repos/conf/svnserve.conf 文件，打开下面配置项
sed -i "s/# anon-access = read/anon-access = none/g"              $REPOS_ROOT/${REPOS_NAME}/conf/svnserve.conf
sed -i "s/# auth-access = write/auth-access = write/g"            $REPOS_ROOT/${REPOS_NAME}/conf/svnserve.conf
sed -i "s/# password-db = passwd/password-db = ..\/..\/etc\/passwd-${REPOS_NAME}_access_by_svn/g"   $REPOS_ROOT/${REPOS_NAME}/conf/svnserve.conf
sed -i "s/# authz-db = authz/authz-db = ..\/..\/etc\/authz-${REPOS_NAME}/g"   $REPOS_ROOT/${REPOS_NAME}/conf/svnserve.conf
#
cp $REPOS_ROOT/${REPOS_NAME}/hooks/pre-revprop-change.tmpl  $REPOS_ROOT/${REPOS_NAME}/hooks/pre-revprop-change
chmod a+x $REPOS_ROOT/${REPOS_NAME}/hooks/pre-revprop-change

#
if test  ! -d  $REPOS_ROOT/etc/; then
    mkdir $REPOS_ROOT/etc
fi

#生成svn配置文件
if test  ! -f  $REPOS_ROOT/etc/passwd-${REPOS_NAME}_access_by_svn; then
    touch $REPOS_ROOT/etc/passwd-${REPOS_NAME}_access_by_svn
cat >$REPOS_ROOT/etc/passwd-${REPOS_NAME}_access_by_svn<<EOF
[users]
mohuifu=mohuifu
EOF
    echoText "created $REPOS_ROOT/etc/passwd-${REPOS_NAME}_access_by_svn" GREEN 
else
    echo "exist $REPOS_ROOT/etc/passwd-${REPOS_NAME}_access_by_svn"
fi

#生成http配置文件
if test  ! -L  $REPOS_ROOT/etc/passwd-${REPOS_NAME}_access_by_http; then
    result=`question "是否创建指向 passwd-common_access_by_http 的链接 ？" 1 yes`
    if [  "$result" = "yes" ] ; then
        ln -svf $REPOS_ROOT/etc/passwd-common_access_by_http $REPOS_ROOT/etc/passwd-${REPOS_NAME}_access_by_http
    else
        touch $REPOS_ROOT/etc/passwd-${REPOS_NAME}_access_by_http
        echoText "created $REPOS_ROOT/etc/passwd-${REPOS_NAME}_access_by_http" GREEN 
    fi
else
    echo "exist $REPOS_ROOT/etc/passwd-${REPOS_NAME}_access_by_http"
fi



if test  ! -f  $REPOS_ROOT/etc/authz-${REPOS_NAME}; then
    touch $REPOS_ROOT/etc/authz-${REPOS_NAME}
cat >$REPOS_ROOT/etc/authz-${REPOS_NAME}<<EOF
[groups]
g_admin=mohuifu
[${REPOS_NAME}:/]
@g_admin = rw
EOF
    echoText "created $REPOS_ROOT/etc/authz-${REPOS_NAME}" GREEN 
else
    echo "exist $REPOS_ROOT/etc/authz-${REPOS_NAME}"
fi


echoText  "Modify $REPOS_ROOT/${REPOS_NAME} property to r/w repos  by http "  GREEN
ac_AuthUserFile=$REPOS_ROOT/etc/passwd-${REPOS_NAME}_access_by_http
ac_AuthzSVNAccessFile=$REPOS_ROOT/etc/authz-${REPOS_NAME}
ac_SVNPath=$REPOS_ROOT/$REPOS_NAME
echoTextRed "
Are you confirm  $ac_AuthUserFile exist?
Please add content below to /etc/apache2/sites-enabled/svn-default
"
echoTextBlue "
-----------------------------------------------------------------------------
#svn reposities config
<VirtualHost `sh_get_ip_address`:80>
ServerName  `sh_get_ip_address`
ServerAdmin mohuifu@gmail.com

DocumentRoot $HOME/www
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /home/gphone/www/>
#  Require valid-user
#  AuthType Basic
#  AuthName \"SVN user password modify\"
#  AuthUserFile     $ac_AuthUserFile
#  Options Indexes
</Directory>

<Location /repositories/$REPOS_NAME>
DAV svn
SVNPath  $ac_SVNPath
AuthType Basic
AuthName \"$REPOS_NAME repository\"
AuthUserFile       $ac_AuthUserFile
AuthzSVNAccessFile $ac_AuthzSVNAccessFile
Require valid-user
</Location>
------------------------------------------------------------------------------
"
result=`question "Are you completely ,if ok,please enter yes？" 1 no`
if [  "$result" = "yes" ] ; then
    echoTextBlue "Restart apache server...."
    sudo /etc/init.d/apache2 restart
fi


export REPOS_ROOT
export REPOS_NAME
return
}

function svn_create_apache2_config()
{
    echoTextGreen "＊＊＊生成 svn 的 apach2 配置文件 ＊＊＊＊"
}


function svn_setup_repos()
{
svn_create_repos
svn_config_repos
}

#创建一个配置文件
function svn_config_apache2()
{
#必须安装 libapache2-svn 
sudo apt-get install libapache2-svn
check_reposes_root
check_repos_name
echoText "＊＊＊配置 apache2 服务器，通过 http 访问 svn ＊＊＊＊"  GREEN
if [ "$REPOS_NAME" = "" ];then
    answer=`question  "将在目录/etc/apache2/sites-enabled 下新建配置文件，请指定文件名："  2  xxx`
    ac_Apache2ConfigFile=svn-$answer
else
    ac_Apache2ConfigFile=svn-$REPOS_NAME
fi

echoText "＊＊＊配置文件为  /etc/apache2/sites-enabled/$ac_Apache2ConfigFile＊＊＊＊" GREEN

#修改ip地址
ipaddress=`sh_get_host_ip`
myip=`question  "为 apache2 虚拟机设定 ip地址："  2   $ipaddress`

ac_Apache2IpAddress=$myip

#############
if [ "$ac_AuthUserFile" = "" ];then
    result=`question "是否为虚拟机指定密码文件？" 1 no`
    if [  "$result" = "yes" ] ; then
        ac_AuthUserFile=`question  "指定密码文件"  2  $REPOS_ROOT/etc/passwd-${REPOS_NAME}_access_by_http`
    fi
fi 

#############
if [ "$ac_AuthzSVNAccessFile" = "" ];then
    result=`question "是否为虚拟机设置授权访问？" 1 no`
    if [  "$result" = "yes" ] ; then
	ac_AuthzSVNAccessFile=`question  "指定授权文件"  2  $REPOS_ROOT/etc/authz-${REPOS_NAME}`
    fi
fi 
ac_SVNPath=$REPOS_ROOT/$REPOS_NAME

export ac_Apache2IpAddress
export ac_SVNPath
export ac_AuthUserFile
export ac_AuthzSVNAccessFile

cat >~/dav_svn.conf<<EOF
#svn reposities config
<VirtualHost $ac_Apache2IpAddress:80>
ServerName  $ac_Apache2IpAddress
ServerAdmin mohuifu@gmail.com

<Location /repositories/$REPOS_NAME>
DAV svn
SVNPath  $ac_SVNPath
AuthType Basic
AuthName "smartphone repository"
#AuthUserFile       $ac_AuthUserFile
#AuthzSVNAccessFile $ac_AuthzSVNAccessFile
#Require valid-user
</Location>
ErrorLog /var/log/apache2/v.repos.error.log
CustomLog /var/log/apache2/v.repos.access.log combined
</VirtualHost>
EOF

####
if [ "$ac_AuthUserFile" != "" ];then
    sed -i "s/#AuthUserFile/AuthUserFile/g" ~/dav_svn.conf
    sed -i "s/#Require valid-user/Require valid-user/g" ~/dav_svn.conf
fi 
####
if [ "$ac_AuthzSVNAccessFile" != "" ];then
    sed -i "s/#AuthzSVNAccessFile/AuthzSVNAccessFile/g" ~/dav_svn.conf
fi 

echoText "请拷贝文件：~/dav_svn.conf 中相应内容到 /etc/apache2/sites-enabled/svn-default" RED
}



function create_lfs_dir()
{
mkdir -pv $1/{bin,boot,dev,{etc/,}opt,home,lib,mnt}
mkdir -pv $1/{proc,media/{floppy,cdrom},sbin,srv,sys}
mkdir -pv $1/var/{lock,log,mail,run,spool}
mkdir -pv $1/var/{opt,cache,lib/{misc,locate},local}
install -dv -m 0750 $1/root
install -dv -m 1777 $1{/var,}/tmp
mkdir -pv $1/usr/{,local/}{bin,include,lib,sbin,src}
mkdir -pv $1/usr/{,local/}share/{doc,info,locale,man}
mkdir -pv $1/usr/{,local/}share/{misc,terminfo,zoneinfo}
mkdir -pv $1/usr/{,local/}share/man/man{1,2,3,4,5,6,7,8}
for dir in $1/usr{,/local}; do
  ln -sfnv share/{man,doc,info} ${dir}
done
}


function create_proj_dir()
{
if [ "$2" = "doc" ]; then
mkdir -pv $1/branch/{REQ,PCE,MMI,HLD,LLD,SOP,Test/{UT,ST,IT,CR},UE/{HR,UES,GUI,MUI}}
mkdir -pv  $1/trunk/{REQ,PCE,MMI,HLD,LLD,SOP,Test/{UT,ST,IT,CR},UE/{HR,UES,GUI,MUI},Reference,PM}
mkdir -pv   $1/tags/{REQ,PCE,MMI,HLD,LLD,SOP,Test}
mkdir -pv $1/release/
mkdir -pv $1/SW_Data/{TF_Data,MMI_SW_Data,TT_SW_Data,ST_SW_Data}

mkdir -pv $1/Third_Part_Soft/branch/{REQ,MMI,ST,Design}
mkdir -pv $1/Third_Part_Soft/trunk/{REQ,MMI,ST,Design,Reference,PM}
mkdir -pv $1/Third_Part_Soft/SW_Data/{MMI_SW_Data,TT_SW_Data}
else
mkdir -pv $1/{3src,branch,trunk,release,tags,special,RDline}
fi
} 


function display_gloab_info_of_svn()
{
echoText "svn仓库配置情况" GREEN
echoText "svn仓库的ip地址：        $ac_Apache2IpAddress" GREEN
echoText "svn仓库的路径：            $ac_SVNPath" GREEN
echoText "svn仓库密码配置文件：  $ac_AuthUserFile" GREEN
echoText "svn仓库授权文件：        $ac_AuthzSVNAccessFile" GREEN
echoText "＊＊＊svn list http://$ac_Apache2IpAddress/$REPOS_NAME＊＊＊＊" GREEN
svn list http://$ac_Apache2IpAddress/$REPOS_NAME
}

function svn_import()
{
echoText "＊＊＊导入项目＊＊＊＊" GREEN
ac_project=`question  "指定要导入的工程"  2  xxxx`
repos_dir=`question  "指定仓库"  2  $default_repos_root/$ac_project`

echo "svn import $PWD/$ac_project  file://$repos_dir"
svn import $PWD/$ac_project  file://$repos_dir -m "Initail import"
}
#更新modem函数

function svn_update_modem()
{
	local update_type
	local result
	local dir_name
	local new_modem_dir
	local project_list
	local select_project
	local platform_name
	local product_name

	
	if is_svn_working_copy `pwd` ;then
		echo "正常工作副本"
	else
		echoText "不是工作副本，请在工程目录下运行" RED>&2
		return 1;
	fi

    if [ $# = 0 ];then
		echoText "手动更新modem" RED>&2
		local TARGET_PRODUCT=`ls build/target/product/*.mk | grep "bbk" | xargs basename | awk -F "." '{print $1}'`
		local MODEM_SRC_BUILD__DBPATH="tst/database_classb_umts"
		local UPDATE_DST="mediatek/custom/common/modem"

		echoText "TARGET_PRODUCT=$TARGET_PRODUCT" BLUE >&2
		echoText "MODEM_SRC_BUILD__DBPATH=$MODEM_SRC_BUILD__DBPATH" BLUE >&2
		echoText "UPDATE_DST=$UPDATE_DST" BLUE >&2

		if [ "$TARGET_PRODUCT" = "bbk73_gb" ];then
			MODEM_SRC_DIR="http://smartphone/repositories/TD1104/trunk/alps-w1140-20111007_modem/out"
		elif [ "$TARGET_PRODUCT" = "bbk75_ics" ];then
			MODEM_SRC_DIR="http://smartphone/repositories/MT6575/trunk/ALPS.ICS.MP.V1_W_20120328_modem/out"
		else
			MODEM_SRC_DIR="http://smartphone/repositories/MT6575/trunk/ALPS.ICS.MP.V1_W_20120328_modem/out"
		fi
		echo "MODEM_SRC_DIR=$MODEM_SRC_DIR" >&2

		result=`svn.shc list $MODEM_SRC_DIR | awk -F "/" '{print $1}'`
		if [ ! -z $result ];then
			result=`getChoice "$TARGET_PRODUCT平台共有modem版本，请选择需要的:" result  1`
			local MODEM_SRC_PATH=$MODEM_SRC_DIR/$result
			echo "MODEM_SRC_PATH=$MODEM_SRC_PATH"
			
			UPDATE_DST_LIST=`ls $UPDATE_DST`
			
			result=`question "是否创建新的modem目录? yes=新建(需要修改CUSTMODEM变量)，no=覆盖已有" 1 no`
			if [ "$result" = "yes" ] ; then
				echoText "请输入新的modem名称，注意格式为：xxx_hspa_xxx" RED >&2
				read dir_name
				while [ "$dir_name" = "" ];do
					echo "输入为空，请重新输入名称"
					read dir_name
				done
				new_modem_dir=$UPDATE_DST/$dir_name
				echo "new_modem_dir=$new_modem_dir"
				if [ ! -d $new_modem_dir ];then
					mkdir $new_modem_dir
					echo "svn co $MODEM_SRC_PATH $new_modem_dir"
					svn.shc co $MODEM_SRC_PATH $new_modem_dir
					
					project_list=`ls mediatek/config/bbk73_gb/ProjectConfig_*.mk`
					select_project="`getChoice "请选择要修改CUSTMODEM的项目" project_list`"
					echoText "sed -i "/CUSTOM_MODEM/c CUSTOM_MODEM = $dir_name" $select_project" BLUE>&2
					sed -i "/CUSTOM_MODEM/c CUSTOM_MODEM = $dir_name" $select_project
					echo "DONE ^_^" 
				else
					echo "目录存在，程序退出"
				fi
			else
				selectdir="`getChoice "Please ensure modem dir:" UPDATE_DST_LIST`"
				echoTextRed "你选择的modem目录是： $selectdir"
				echo "rm -rvf $UPDATE_DST/$selectdir/*"
				echo "svn.shc co $MODEM_SRC_PATH $UPDATE_DST/$selectdir"
			fi
		else
			echoText "版本库中无modem，请检查$MODEM_SRC_DIR" RED>&2
			return 1
		fi
	else
	
		echo "自动更新modem，传入参数：平台名称，项目名称。默认使用svn上最新的modem，默认替换项目使用的modem目录和配置文件"
		
	fi

}

function svn_update_thirdparty()
{
	SRC_SVN="http://smartphone/repositories/ThirdParty/Apks"
	CONFIG_PATH="vendor/bbk/common/3Party"
	#读取机型信息
	BBK_PRODUCT_MODEL=`cat mediatek/config/bbk73_gb/ProjectConfig.mk | grep "BBK_PRODUCT_MODEL =" | awk '{print $3}'`
        echo "fffffffffffffffffffffffffffffffffffff"
        echo $BBK_PRODUCT_MODEL
	CONFIG_FILE_NAME=${BBK_PRODUCT_MODEL}_Config.txt
	CONFIG_FILE=${CONFIG_PATH}/${CONFIG_FILE_NAME}

	#从第三方仓初始化到框架
	rm -rvf $CONFIG_PATH
	svn.shc co $SRC_SVN $CONFIG_PATH

	#删除在配置文件中不存在，但是在版本库中的存在的apk，做到不同项目中，有的apk并不使用
	apk_in_repo=`ls $CONFIG_PATH | grep "apk"`
	apk_in_config_file=`cat $CONFIG_FILE  | grep apk | awk -F '=' '{print $1}'`

	for i in $apk_in_repo ;do
	   result=`strIsSubstring "$i" "$apk_in_config_file"`
	   if [ "$result" -eq "0" ];then
		   echo "rm -vf ${CONFIG_PATH}/${i}"
		   rm -vf ${CONFIG_PATH}/${i}
	   else
		   echo "i=$i,result=1，不需要删除此apk"
	   fi
	done
	#按机型读取配置文件并更新到配置文件中apk的对应版本
	for var in `cat $CONFIG_FILE`;do
		  apk_name=`echo $var | awk -F '=' '{print $1}'`
		  apk_svn_version=`echo $var | awk -F '=' '{print $2}'`

		  #echo "${CONFIG_PATH}/${apk_name}"
		  echo "svn.shc update ${SRC_SVN}/${apk_name} -r ${apk_svn_version}"
		  svn.shc up ${CONFIG_PATH}/${apk_name} -r ${apk_svn_version}
	done
	#更新android.mk文件
	echo "CONFIG_FILE_NAME=$CONFIG_FILE_NAME"
	echo "CONFIG_FILE=$CONFIG_FILE"
	echo "mv ${CONFIG_PATH}/Android_${BBK_PRODUCT_MODEL}.mk ${CONFIG_PATH}/Android.mk"
	mv ${CONFIG_PATH}/Android_${BBK_PRODUCT_MODEL}.mk ${CONFIG_PATH}/Android.mk
	echo "rm ${CONFIG_PATH}/Android_*.mk"
	rm -vf ${CONFIG_PATH}/Android_*.mk
}

